package com.zego.wawaji.ui.center.user;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.album.ImgSelectUtil;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.base.BasePanel;
import com.zego.wawaji.ui.bean.user.EditKey;
import com.zego.wawaji.ui.bean.user.UserDetail;
import com.zego.wawaji.ui.dialog.EditPopupWindow;
import com.zego.wawaji.ui.dialog.LoadingDialog;

/**
 * 个人中心Head
 */
public class UserCenterHead extends BasePanel implements View.OnClickListener, ImgSelectUtil.OnChooseCompleteListener {
    private ImageView head;
    private TextView name;
    private View edit_top;

    public UserCenterHead(Context context) {
        super(context);
        setContentView(R.layout.user_center_head);
        initView();
    }

    private void initView() {
        UserDetail userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        head = (ImageView) findViewById(R.id.user_head);
        name = (TextView) findViewById(R.id.user_nick);
        TextView id = (TextView) findViewById(R.id.user_id);

        ImageLoader.loadCircleAvatar(getContext(), userDetail.getAvatar(), head);
        name.setText(userDetail.getNickname());
        edit_top = findViewById(R.id.edit_top);
        id.setText("ID: " + userDetail.getUid());

        head.setOnClickListener(this);
        name.setOnClickListener(this);
        findViewById(R.id.center_back).setOnClickListener(this);
    }

    public void refresh() {
        UserDetail userDetail = ModuleMgr.getCenterMgr().getMyInfo();

        ImageLoader.loadCircleAvatar(getContext(), userDetail.getAvatar(), head);
        name.setText(userDetail.getNickname());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_head:
                ImgSelectUtil.getInstance().pickPhoto(getContext(), this);
                break;

            case R.id.user_nick:
                EditPopupWindow popupWindow = new EditPopupWindow(getContext(), EditKey.s_key_nickName, name);
                popupWindow.showPopupWindow(edit_top);
                break;

            case R.id.center_back:
                getActivity().finish();
                break;
        }
    }

    @Override
    public void onComplete(String... path) {
        if (path == null || path.length == 0 || TextUtils.isEmpty(path[0])) {
            return;
        }

        LoadingDialog.show((FragmentActivity) getContext(), "正在上传头像");
        ModuleMgr.getCenterMgr().uploadAvatar(path[0], null);
    }
}
